package com.whfc.mach.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.mach.dao.AppMachDictMapper;
import com.whfc.mach.dto.MachDictDataDTO;
import com.whfc.mach.entity.AppMachDict;
import com.whfc.mach.param.MachDictDataAddParam;
import com.whfc.mach.param.MachDictDataEditParam;
import com.whfc.mach.service.AppMachDictDataService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

/**
 * @author sun_guodong
 * @date 2021-08-11
 */
@DubboService(interfaceClass = AppMachDictDataService.class, version = "1.0.0")
public class AppMachDictDataServiceImpl implements AppMachDictDataService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppMachDictMapper appMachDictMapper;

    /**
     * 全局的工种所在的deptId
     */
    private static final Integer DEPT_ID = 0;

    @Override
    public PageData<MachDictDataDTO> list(Integer deptId, String dictCode, Integer pageNum, Integer pageSize) {
        logger.info("字典数据列表(分页)服务,deptId:{},dictCode:{},pageNum:{},pageSize:{}", deptId, dictCode, pageNum, pageSize);
        Integer pid = appMachDictMapper.selectByCode(dictCode);
        if (pid == null) {
            return PageUtil.pageData(PageInfo.of(Collections.EMPTY_LIST));
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MachDictDataDTO> list = appMachDictMapper.selectByDeptIdAndPid(deptId, pid);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<MachDictDataDTO> list(Integer deptId, String dictCode) {
        logger.info("字典数据列表(不分页)服务,deptId:{},dictCode:{}", deptId, dictCode);
        Integer pid = appMachDictMapper.selectByCode(dictCode);
        if (pid == null) {
            return new ListData<>(Collections.EMPTY_LIST);
        }
        List<MachDictDataDTO> list = appMachDictMapper.selectByDeptIdAndPid(deptId, pid);
        if (list.size() == 0) {
            list = appMachDictMapper.selectByDeptIdAndPid(DEPT_ID, pid);
            if (list.size() != 0) {
                appMachDictMapper.batchInsert(deptId, pid, list);
            }
            List<MachDictDataDTO> result = appMachDictMapper.selectByDeptIdAndPid(deptId, pid);
            return new ListData<>(result);
        }
        return new ListData<>(list);
    }

    @Override
    public void add(MachDictDataAddParam param) {
        logger.info("添加字典数据服务，param:{}", param.toString());
        Integer deptId = param.getDeptId();
        String name = param.getName();
        String dictCode = param.getDictCode();
        Integer pid = appMachDictMapper.selectByCode(dictCode);
        if (pid == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该数据字典不存在");
        }
        Integer id = appMachDictMapper.selectByDeptIdAndName(deptId, pid, name);
        if (id != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典数据已存在,不能重复添加");
        }
        AppMachDict record = new AppMachDict();
        record.setName(name);
        record.setDeptId(deptId);
        record.setPid(pid);
        record.setRemark(param.getRemark());
        appMachDictMapper.insertSelective(record);
    }

    @Override
    public void edit(MachDictDataEditParam param) {
        logger.info("编辑字典数据服务，param:{}", param.toString());
        String name = param.getName();
        Integer id = param.getId();
        AppMachDict appMachDict = appMachDictMapper.selectByPrimaryKey(id);
        if (appMachDict == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典数据不存在");
        }
        Integer deptId = appMachDict.getDeptId();
        Integer pid = appMachDict.getPid();
        Integer id1 = appMachDictMapper.selectByDeptIdAndName(deptId, pid, name);
        if (id1 != null && !id1.equals(id)) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典数据已存在,不能重复添加");
        }
        AppMachDict record = new AppMachDict();
        record.setId(id);
        record.setName(name);
        record.setRemark(param.getRemark());
        appMachDictMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void del(Integer id) {
        logger.info("删除字典数据服务,itemId:{}", id);
        appMachDictMapper.deleteLogic(id);
    }
}
